package com.kime.email.spring.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
@Slf4j
public class MailServiceImpl implements MailService{

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.properties.from}")
    private String from;

    @Override
    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);
        try {
            javaMailSender.send(simpleMailMessage);
            log.info("简单邮件发送成功");
        } catch (Exception e) {
            log.error("简单邮件发送异常", e);
        }
    }

    @Override
    public void sendMimeMessagePreparatorEmail(String to, String subject, String content) {
        MimeMessagePreparator mimeMessagePreparator = mimeMessage -> {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
        };
        try {
            javaMailSender.send(mimeMessagePreparator);
            log.info("邮件发送成功");
        } catch (Exception e) {
            log.error("邮件发送异常", e);
        }
    }


    @Override
    public void sendHtmlEmail(String to, String subject, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            javaMailSender.send(message);
            log.info("HTML 邮件发送成功");
        } catch (MessagingException e) {
            log.error("HTML 邮件发送异常", e);
        }
    }

    @Override
    public void sendAttachmentEmail(String to, String subject, String content, String filePath) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            String filename = fileSystemResource.getFilename();
            assert filename != null;
            // 可使用 addAttachment 方法添加多个附件
            mimeMessageHelper.addAttachment(filename, fileSystemResource);
            javaMailSender.send(message);
            log.info("带附件的邮件发送成功");
        } catch (MessagingException e) {
            log.error("带附件的邮件发送异常", e);
        }
    }

    @Override
    public void sendInLineResourceEmail(String to, String subject, String content, String resourcePath, String resourceId) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content, true);
            FileSystemResource fileSystemResource = new FileSystemResource(new File(resourcePath));
            // 可添加多个静态资源
            mimeMessageHelper.addInline(resourceId, fileSystemResource);
            javaMailSender.send(message);
            log.info("带静态资源的邮件发送成功");
        } catch (MessagingException e) {
            log.error("带静态资源的邮件发送异常", e);
        }
    }

    @Override
    public boolean sendTemplateEmail(String to, String subject, String emailContent) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(emailContent, true);
            FileSystemResource fileSystemResource = new FileSystemResource(new File("D:\\workspace\\email-demo\\src\\main\\resources\\static\\image\\JiangYi-logo.png"));
            mimeMessageHelper.addInline("logoImage", fileSystemResource);
            FileSystemResource fileSystemResource2 = new FileSystemResource(new File("D:\\workspace\\email-demo\\src\\main\\resources\\static\\image\\jiangyi.png"));
            mimeMessageHelper.addInline("TimgImage", fileSystemResource2);
            javaMailSender.send(message);
            log.info("HTML 邮件发送成功");
            return true;
        } catch (MessagingException e) {
            log.error("HTML 邮件发送异常", e);
            return false;
        }
    }


}
